<?php

abstract class Component extends ObjLib\Object {	

	# Whether this component currently has a container which it will render itself inside.
	public $container = false;
	
	# The active template for rendering this component.
	public $activeTemplate = 'template';

	public static $classList = array (
		'Page',
		'Article',
		'Nav'
	);
		
	# valid actions for this component
	public static $actions = array ();
		
	function render () {
		$this->loadPositions ();
		if ($this->container) {
			$this->template ();
		}
		else { # If the component has no container, render it with its own style
			$this->style ();
		}
		return true;
	}
	
	function process ($params = array ()) {
		for ($i = 0; $i < count ($params); $i++) {
			if ($i % 2 != 0) {
				$_REQUEST[$params[$i-1]] = $params[$i];
			}
		}
		$this->render ();
		return true;
	}
	
	function processForm ($params = array ()) {
		for ($i = 0; $i < count ($params); $i++) {
			if ($i % 2 != 0) {
				$_REQUEST[$params[$i-1]] = $params[$i];
			}
		}
		if (isset($_REQUEST['comId']) && isset($_REQUEST['comClass']) && isset($_REQUEST['comAction'])) {
			if ($_REQUEST['comId'] == 'NEW') {
				$actionComponent = Component::getInstance ('', $_REQUEST['comClass']);
			}
			else {
				$actionComponent = Component::getInstance ($_REQUEST['comId'], $_REQUEST['comClass']);
			}
			if ($actionComponent->isRegisteredAction ($_REQUEST['comAction'])) {
				$actionComponent->{$_REQUEST['comAction']} ();
				$_REQUEST['comId'] = $actionComponent->id;
			}
		}
		$this->render ();
	}
	
	function style () {
			global $stylesDir;
			global $rootNavId;
			global $rootUrl;
			global $rootDir;
			global $dbh;
			
			$fileName = $stylesDir;
		
			if (!empty($this->style)) {
				$fileName .= "/{$this->style}";
			}
			else {
				$fileName .= '/default_style.php';
			}
			include $fileName;
	}
	
	function template() {
		global $templatesDir;
		
		$fileName = $templatesDir . '/' . get_class ($this);
		
		if (!empty($this->{$this->activeTemplate})) {
			$fileName .= "/{$this->{$this->activeTemplate}}";
		}
		else {
			$fileName .= "/default_{$this->activeTemplate}.php";
		}
		include $fileName;
	}
	
	function position ($title) {
		if (isset($this->positions[$title])) {
			$this->positions[$title]->container = $this;
			return $this->positions[$title]->render ();
		}
	}
	
	function loadPositions () {
		return;
	}
	
	function isRegisteredAction ($action) {
		if ($action == 'render' || $action == 'process' || $action == 'processForm') {
			return true;
		}
		else {
			return (array_search ($action, static::$actions) !== false);
		}
	}
	
	function loadFormParams ( $method = 'REQUEST' ) {
		foreach ($this->params as $param) {
			$this->{$param} = eval ("return \$_{$method}['{$param}'];");
		}
	}
	
	function getInstance ($id, $className) {
		return eval ("return new {$className} ('{$id}');");
	}	
		
	function editForm () {
		$this->activeTemplate = 'editFormTemplate';
		echo $this->render ();
	}
	
	function editFormSave () {
		$this->loadFormParams ( 'REQUEST' );
		$this->save ();
	}
	
	static function styleSelector ($fieldName) {
		global $stylesDir;
		global $templatesDir;
		
		$folders = FS::getSubFolders ($stylesDir);
		$files = FS::getFiles ($stylesDir, array ('php', 'inc'));

		include $templatesDir . '/styleSelector.php';
	}
	
	static function templateSelector ($className, $fieldName) {
		global $templatesDir;
		
		$files = FS::getFiles ($templatesDir . '/' . $className, array ('php', 'inc'));

		include $templatesDir . '/templateSelector.php';
	}
	
	static function componentSelector ($idFieldName, $classFieldName) {
		global $templatesDir;
		
		$firstClassComponents = Component::getClassComponents (Component::$classList[0]);		
		include $templatesDir . '/componentSelector.php';
	}
	
	static function getClassComponents ($className) {
		$query = new ObjLib\Query (array (
			'tables' => $className
		));
		return $query->select ();
	}
}

?>